package mhys.Dao;
import mhys.Entity.User;
import mhys.Util.DBUtil;
import java.sql.SQLException;

public class LoginDao extends BaseDao{
    //判断用户名
    public User getUser(String name){
        String sql = "select u_id,username,password,email from user where username = ?";
        try {
            res = selectLogin(sql,name);
            while (res.next()){
                Integer id = res.getInt("u_id");
                String username = res.getString("username");
                String password = res.getString("password");
                String email = res.getString("email");
                User user = new User(id, username, password,email);
                return user;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(res,stmt,conn);
        }
        return null;
    }
    public User findUser(String qqEmail){
        String sql = "select u_id,username,password,email from user where email = ?";
        try {
            res = selectLogin(sql,qqEmail);
            while (res.next()){
                Integer id = res.getInt("u_id");
                String username = res.getString("username");
                String password = res.getString("password");
                String email = res.getString("email");
                User user = new User(id, username, password,email);
                return user;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(res,stmt,conn);
        }
        return null;
    }
    public int resetPassword(String newPassword){
        String sql = "update user set password = ?";
        return insertUpdateDelete(sql,newPassword);
    }

    public static void main(String[] args) {
        LoginDao loginDao = new LoginDao();
        System.out.println(loginDao.findUser("1605030389@qq.com"));


    }
}
